package com.musemun;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.musemun.mapper.ErMapper;
import com.musemun.mapper.RoleMapper;
import com.musemun.pojo.*;
import com.musemun.service.*;

import com.musemun.util.JwtTokenUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

//
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringWebStarterxx {

    @Autowired
    PermissionService permissionService;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    ErService epService;
    @Resource
    RoleService roleService;
    @Resource
    RolePermissionService rolePermissionService;
    //    @Test
//    public void insertPermission(){
//        permissionService.list(new QueryWrapper<>());
//    }
    @Test
    public void readTxt1() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\2.txt";
        Set<Role> set = new HashSet<>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");
                Role role = new Role();
                role.setRole(s[0]);
                set.add(role);
            }
            ArrayList<Role> list1 = new ArrayList<>(set);
            roleService.saveBatch(list1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void readTxt2() {
        String filePath = "C:\\Users\\Administrator\\Desktop\\2.txt";
        Set<Permission> set =new HashSet<>();

        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");
                Permission permission = new Permission();
                permission.setUrl("/"+s[1]);
                permission.setPName(s[2]);
                set.add(permission);
            }
            List<Permission> permissionList = new ArrayList<>(set);
            permissionService.saveBatch(permissionList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void readTxt3() {

        List<Employee> list = employeeService.list();
        List<Role> list1 = roleService.list();
        List<Er>list2 =new ArrayList<>();
        list.forEach(data->{
            for (Role role : list1) {
                if(data.getRoles().equals(role.getRole())){
                    list2.add(new Er(data.getENo(),role.getRid()));
                }
            }
        });
        epService.saveBatch(list2);
    }

    @Resource
    RpService rpService;
    @Test
    public void readTxt4(){
        String filePath = "C:\\Users\\Administrator\\Desktop\\2.txt";
        ArrayList<Rp> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] s = line.split(" ");

                Role rid = roleService.getOne(new QueryWrapper<Role>().eq("role", s[0]));
                Permission pid = permissionService.getOne(new QueryWrapper<Permission>().eq("pName", s[2]));
//                Rp rp = new Rp().setRid(rid).setRid(pid);
                System.out.println("==========="+rid);
                System.out.println("====+======="+pid);
                if(rid!=null&&pid!=null){
                    Rp rp = new Rp(rid.getRid(),pid.getPId());
                    list.add(rp);
                }

            }
        rpService.saveBatch(list);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.out.println(list);
        }
    }

    @Test
    public void readTxt5(){
        List<Role> list = roleService.list();
        List<Er> list1=new ArrayList<>();
        list.forEach(data->{
            Er er = new Er(1, data.getRid());
            list1.add(er);
        });
        epService.saveBatch(list1);
    }

    @Test
    public void readDatabase(){
        readTxt1();
        readTxt2();
        readTxt3();
        readTxt4();
        readTxt5();
    }

    @Test
    public void readTxt6()
    {
        Role role = new Role("超级管理员");
        roleService.save(role);
        List<Permission> list = permissionService.list();
        List<Rp> list1=new ArrayList<>();
        list.forEach(data->{
            Rp rp = new Rp(role.getRid(), data.getPId());
            list1.add(rp);
        });
        rpService.saveBatch(list1);
    }
    @Resource
    ErMapper erMapper;
    @Test
    public void deleteTest(){
        erMapper.deleteErByEnoAndRole(7,"管理员");
    }
}
